//
//  NewCaseViewController.m
//  MyDIYHouse
//
//  Created by dlios on 15-7-3.
//  Copyright (c) 2015年 李平洋. All rights reserved.
//

#import "NewCaseViewController.h"
#import "CaseCell.h"
#import "CaseModel.h"

#import "HTTPTool.h"

#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "NewCaseDetailViewController.h"
#include "MJRefresh.h"
#import "StylistDetailViewController.h"


@interface NewCaseViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UIButton *styleButton;
@property(nonatomic,retain)UIButton *kindButton;
@property(nonatomic,retain)UIButton *areaButton;
@property(nonatomic,retain)UIButton *moneButton;


@property(nonatomic,retain)UITableView *CaseTableView;
//接受数据的数组
@property(nonatomic,retain)NSMutableArray *CaseArr;


//把几个参数写成属性
@property(nonatomic,assign)NSInteger start;
@property(nonatomic,copy)NSString *count;
@property(nonatomic,copy)NSString *area;
@property(nonatomic,copy)NSString *style;
@property(nonatomic,copy)NSString *type;
@property(nonatomic,copy)NSString *cost;
@property(nonatomic,copy)NSString *user;
@end

@implementation NewCaseViewController
- (void)dealloc
{
    self.CaseTableView.delegate = nil;
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.CaseArr=[NSMutableArray array];
        self.start=0;
        self.count=@"5";
        self.area=@"0";
        self.style=@"0";
        self.type=@"0";
        self.cost=@"0";
        self.user=@"null";
        [self createData];
        
    }
    return self;
}

-(void)createData {
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/project?start=%ld&count=%@&area=%@&style=%@&type=%@&cost=%@&user=%@&", self.start, self.count, self.area, self.style, self.type, self.cost, self.user];
    
    [HTTPTool get:urlStr body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *projectsArr = dic[@"projects"];
        for (NSDictionary *tempDic in projectsArr) {
            CaseModel *caseM = [[CaseModel alloc] initWithDic:tempDic];
            // NSLog(@"%@",caseM.title);
            
            [self.CaseArr addObject:caseM];
            
        }
        
        [self.CaseTableView reloadData];
        [self.CaseTableView footerEndRefreshing];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"案例";
    self.navigationController.navigationBar.translucent = NO;
    self.CaseTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height-64-49) style:UITableViewStyleGrouped];
    self.CaseTableView.backgroundColor=[UIColor whiteColor];
    self.CaseTableView.delegate=self;
    self.CaseTableView.dataSource=self;
    
    self.CaseTableView.rowHeight=210;
    [self.view addSubview:self.CaseTableView];
    [_CaseTableView release];
    
    [self.CaseTableView addFooterWithCallback:^{
        self.start = self.start+5;
        [self createData];
    }];
    
    
    //创建四个button
    self.styleButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.styleButton.layer.borderWidth=1;
    self.styleButton.frame=CGRectMake(0, 0, self.view.frame.size.width/4, 50);
    //    self.styleButton.titleLabel.text=@"风格";
    [self.styleButton setTitle:@"风格" forState:UIControlStateNormal];
    [self.styleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.styleButton.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.styleButton];
    //
    self.kindButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.kindButton.layer.borderWidth=1;
    self.kindButton.frame=CGRectMake(self.view.frame.size.width/4, 0, self.view.frame.size.width/4, 50);
    //    self.kindButton.titleLabel.text=@"户型";
    [self.kindButton setTitle:@"户型" forState:UIControlStateNormal];
    [self.kindButton  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.kindButton.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.kindButton];
    //
    self.areaButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.areaButton.layer.borderWidth=1;
    self.areaButton.frame=CGRectMake(self.view.frame.size.width/4*2, 0, self.view.frame.size.width/4, 50);
    //    self.areaBUtton.titleLabel.text=@"面积";
    [self.areaButton setTitle:@"面积" forState:UIControlStateNormal];
    [self.areaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.areaButton.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.areaButton];
    //
    self.moneButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.moneButton.layer.borderWidth=1;
    self.moneButton.frame=CGRectMake(self.view.frame.size.width/4*3, 0, self.view.frame.size.width/4, 50);
    //    self.moneyBUtton.titleLabel.text=@"预算";
    [self.moneButton setTitle:@"预算" forState:UIControlStateNormal];
    [self.moneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.moneButton.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.moneButton];
    
    
    
    
}

//casetableView的两个协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CaseArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@"reuse";
    CaseCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[[CaseCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        
    }
    
    CaseModel *caseClass = self.CaseArr[indexPath.row];
    NSDictionary *tempDic=[caseClass.photos firstObject];
    //    NSString *titleStr=[tempDic objectForKey:@"title"];
    NSNumber *picId=[tempDic objectForKey:@"id"];
    NSNumber *picWidth=[tempDic objectForKey:@"width"];
    NSNumber *picHeight=[tempDic objectForKey:@"height"];
    NSString *picUrl=[NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",picId,picWidth,picHeight];
    
    //图片
    [cell.furnitureImage sd_setImageWithURL:[NSURL URLWithString:picUrl]];
    //日期
    cell.dateLable.text =caseClass.date;
    cell.likeNumLable.text = [caseClass.likeNum stringValue];
    cell.commentNumLable.text = [caseClass.commentNum stringValue];
    [cell.peopleImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:[caseClass.user objectForKey:@"userImage"][@"large"]] forState:UIControlStateNormal];
    cell.peopleImageButton.tag = 10000+indexPath.row;
    [cell.peopleImageButton addTarget:self action:@selector(peopleImageButtonclick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    cell.titleLable.text=caseClass.title;
    return cell;
    
}

- (void)peopleImageButtonclick:(UIButton *)button
{
    NSInteger row = button.tag -10000;
    
    StylistDetailViewController *styDetailVC = [[StylistDetailViewController alloc] init];
    
    //  StylistModel *stylist = self.stylistArr[indexPath.row];
    CaseModel *caseClass = self.CaseArr[row];
    
    
    styDetailVC.myUserName = [caseClass.user objectForKey:@"userName"];
    [self.navigationController pushViewController:styDetailVC animated:YES];
    
    [styDetailVC release];
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewCaseDetailViewController *caseDetailVC = [[NewCaseDetailViewController alloc] init];
    caseDetailVC.myCase = self.CaseArr[indexPath.row];
    
    [self.navigationController pushViewController:caseDetailVC animated:YES];
    [caseDetailVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
